package com.jereh.music.acitivities;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.RemoteViews;

import com.jereh.music.R;
import com.jereh.music.base.BaseActivity;
import com.jereh.music.constants.Constants;
import com.jereh.music.entity.Bitrate;
import com.jereh.music.entity.Song;
import com.jereh.music.entity.SongInfo;
import com.jereh.music.service.PlayService;

import java.util.ArrayList;
import java.util.List;

public class PlayActivity extends BaseActivity implements View.OnClickListener{

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play);
        findViewById(R.id.play).setOnClickListener(this);
        intent=new Intent(this, PlayService.class);
    }
    private void initSongList() {
        Song song=new Song();
        Bitrate bitrate=new Bitrate();
        bitrate.setFile_link("http://yinyueshiting.baidu.com/data2/music/42783748/42783748.mp3?xcode=f8892c3c8e1752c0c9bf3d409e407d1c");
        SongInfo info=new SongInfo();
        info.setTitle("海阔天空");
        song.setBitrate(bitrate);
        song.setSonginfo(info);
        Song song2=new Song();
        Bitrate bitrate2=new Bitrate();
        bitrate2.setFile_link("http://yinyueshiting.baidu.com/data2/music/f82dd0820651c18860dced6d44d98681/277581711/277581711.mp3?xcode=23a4d936fc1a6b4c0d9b8f0c677c76d2");
        SongInfo info2=new SongInfo();
        info2.setTitle("你在就好");
        song2.setBitrate(bitrate2);
        song2.setSonginfo(info2);
        Song song3=new Song();
        Bitrate bitrate3=new Bitrate();
        bitrate3.setFile_link("http://yinyueshiting.baidu.com/data2/music/6d0f1b7d5070e3e76c60ed4bfae13edc/278862684/278862684.mp3?xcode=e96de29b7a2d14bb9e9cdd6067ee2141");
        SongInfo info3=new SongInfo();
        info3.setTitle("长大的童话");
        song3.setBitrate(bitrate3);
        song3.setSonginfo(info3);
        List<Song> list=new ArrayList<>();
        list.add(song);
        list.add(song2);
        list.add(song3);
        application.music.initMusic(list);
    }

    @Override
    public void onClick(View v) {
        //开启一个服务
        initService();
        //在状态栏上显示播放器
        initNotification();
    }

    private void initService() {
        startService(intent);
        bindService(intent,connection,BIND_AUTO_CREATE);
    }

    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            application.music=(PlayService.PlayBinder)service;
            initSongList();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    private void initNotification(){
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.notification_play);
        Notification notification=new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContent(remoteViews)
                .build();
        //给按钮添加绑定广播
        Intent playIntent=new Intent("com.jereh.music.play");
        PendingIntent playPending=PendingIntent.getBroadcast(this,0,playIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.play,playPending);

        Intent pauseIntent=new Intent("com.jereh.music.pause");
        PendingIntent pausePending=PendingIntent.getBroadcast(this,0,pauseIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.pause,pausePending);

        Intent nextIntent=new Intent("com.jereh.music.next");
        PendingIntent nextPending=PendingIntent.getBroadcast(this,0,nextIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.next,nextPending);

        Intent prevIntent=new Intent("com.jereh.music.prev");
        PendingIntent prevPending=PendingIntent.getBroadcast(this,0,prevIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.prev,prevPending);

        Intent cancelIntent=new Intent("com.jereh.music.cancel");
        PendingIntent cancelPending=PendingIntent.getBroadcast(this,0,cancelIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.cancel,cancelPending);
        application.notiManager.notify(Constants.PLAY_NOTI,notification);
    }

    @Override
    protected void onDestroy() {
        unbindService(connection);
        super.onDestroy();
    }
}
